import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/home/duty/schdule_info_create_entity.dart';
import 'package:safetymonitor/app/data/entities/home/execute_entity.dart';
import 'package:safetymonitor/modules/home/home_page/duty_manager/duty/controller.dart';
///开始排班
class StartSchedulingController extends GetxController {
  ///提交到后台的实体类
  List<SchduleInfoCreateEntity> confirmList = [];

  /// 值班人员实体类
  List<ExecuteTicketReceiptListOperatorLists> operatorLists = [];

  /// 值班人员领导实体类
  List<ExecuteTicketReceiptListOperatorLists> operatorListsL = [];

  /// 班次名称
  String shiftName = '';
  /// 班次名称id
  int shiftNameId = 0;

  /// 值班室电话
  String dutyPhone = '';

  /// 值班结束时间
  String endDate = '';

  /// 值班开始时间
  String startDate = '';
  /// 默认开始时间  null传入也是开始时间
  DateTime? startTime = DateTime.now();
  selectTime() async {
    startTime = await showDatePicker(
      context: Get.context!,
      initialDate: startTime,
      firstDate: DateTime(2000),
      lastDate: DateTime(2100),
      helpText: '请选择值班开始时间',
    );
    if (startTime != null) {
      startDate =
          Jiffy.parseFromDateTime(startTime!).format(pattern: 'yyyy-MM-dd');
      update();
      _selectEndTime();
    }
  }

  ///结束时间选取
  _selectEndTime() async {
    final DateTime? endTime = await showDatePicker(
        context: Get.context!,
        initialDate: startTime,
        firstDate: DateTime(2000),
        lastDate: DateTime(2100),
        helpText: '请选择值班结束时间');
    if (endTime != null) {
      endDate = Jiffy.parseFromDateTime(endTime).format(pattern: 'yyyy-MM-dd');
      update();
    }
  }

  ///创建排班信息
  void confirm() async {
    //班次名称
    if (shiftName.trim().isEmpty) {
      AppToast.showWarning('请输入班次名称');
      return;
    }
    //值班室电话
    if (dutyPhone.trim().isEmpty) {
      AppToast.showWarning('请输入值班室电话');
      return;
    }
    //值班日期(开始时间)
    if (startDate.trim().isEmpty) {
      AppToast.showWarning('请输入值班开始日期');
      return;
    }
    //值班日期(结束时间)
    if (endDate.trim().isEmpty) {
      AppToast.showWarning('请输入值班结束日期');
      return;
    }
    if (operatorLists.isEmpty) {
      AppToast.showWarning('请输入值班人员信息');
      return;
    }
    if (operatorListsL.isEmpty) {
      AppToast.showWarning('请输入值班领导信息');
      return;
    }
    confirmList.clear();
    //将班次名称，值班室电话，值班日期塞入 confirmList 的每一个元素中；
    for (int i = 0; i < operatorLists.length; i++) {
      final SchduleInfoCreateEntity schduleInfoCreateEntity = SchduleInfoCreateEntity();
      schduleInfoCreateEntity.dutyType = 1; //值班人员1 值班领导 2
      schduleInfoCreateEntity.scheduleName = shiftName; //班次名称
      schduleInfoCreateEntity.scheduleConfigId = shiftNameId; //班次名称
      schduleInfoCreateEntity.dutyEndDate = endDate; //值班日期
      schduleInfoCreateEntity.dutyStartDate = startDate; //值班日期
      schduleInfoCreateEntity.dutyRoomLandline = dutyPhone; //值班室电话
      if (operatorLists[i].operatorUserName!.isEmpty) {
        AppToast.showWarning('请输入值班人员');
        return;
      }
      schduleInfoCreateEntity.dutyPerson = operatorLists[i].operatorUserName; //值班人员
      schduleInfoCreateEntity.dutyPersonId = int.tryParse(operatorLists[i].operatorUserId ?? '0'); //值班人员 id
      if (operatorLists[i].operatorUserSign!.isEmpty) {
        AppToast.showWarning('请输入班次类型');
        return;
      }
      // schduleInfoCreateEntity.personScheduleTypeCode = operatorLists[i].operatorUserSign; //班次类型
      schduleInfoCreateEntity.personScheduleTypeCode = operatorLists[i].ticketPlanId; //班次类型id
      schduleInfoCreateEntity.dutyPersonPhone = operatorLists[i].phone; //值班人员电话
      confirmList.add(schduleInfoCreateEntity);
    }
    for (int i = 0; i < operatorListsL.length; i++) {
      final SchduleInfoCreateEntity schduleInfoCreateEntity = SchduleInfoCreateEntity();
      schduleInfoCreateEntity.dutyType = 2; //值班人员1 值班领导 2
      schduleInfoCreateEntity.scheduleName = shiftName; //班次名称
      schduleInfoCreateEntity.scheduleConfigId = shiftNameId; //班次名称
      schduleInfoCreateEntity.dutyEndDate = endDate; //值班日期
      schduleInfoCreateEntity.dutyStartDate = startDate; //值班日期
      schduleInfoCreateEntity.dutyRoomLandline = dutyPhone; //值班室电话
      if (operatorListsL[i].operatorUserName!.isEmpty) {
        AppToast.showWarning('请输入值班领导');
        return;
      }
      schduleInfoCreateEntity.dutyLeader = operatorListsL[i].operatorUserName; //值班领导
      schduleInfoCreateEntity.dutyLeaderId = int.tryParse(operatorListsL[i].operatorUserId ?? '0'); //值班领导 id
      if (operatorListsL[i].operatorUserSign!.isEmpty) {
        AppToast.showWarning('请输入值班领导班次类型');
        return;
      }
      // schduleInfoCreateEntity.leaderScheduleTypeCode = operatorListsL[i].operatorUserSign; //班次类型
      schduleInfoCreateEntity.leaderScheduleTypeCode = operatorListsL[i].ticketPlanId; //班次类型 id
      schduleInfoCreateEntity.dutyLeaderPhone = operatorListsL[i].phone; //值班领导电话
      confirmList.add(schduleInfoCreateEntity);
    }

    final res = await HttpUtil()
        .generatePostRequest(
          Interface.createScheduleInfo,
          data: confirmList,
        )
        .execute(isShowLoading: false, isShowToast: false);
    res.onSuccess((result) async {
      if (result.data != null) {
        Get.back();
        AppToast.showSuccess('创建成功~');
        Get.find<DutyMgmtPageController>().getDayInfoList();
        Get.find<DutyMgmtPageController>().getMonthEvent();
      }
    });
    res.onError((e) {
      AppToast.showError(e.message);
    });
  }
}
